<!DOCTYPE html>
<html>
<head>
<title>Solenoid in a Magnetic Field</title>
</head>
<body>
<canvas id="physicsCanvas" width="650" height="400"></canvas>
<script>
const canvas = document.getElementById('physicsCanvas');
const ctx = canvas.getContext('2d');

// Style settings
ctx.lineWidth = 1.5;
ctx.strokeStyle = 'black';
ctx.fillStyle = 'black';
ctx.font = '18px Arial';

// --- Magnetic Field ---
const fieldY1 = 150;
const fieldY2 = 175;
const fieldX_start = 40;
const fieldX_end = 180;
const arrowSize = 8;

// Arrow 1
ctx.beginPath();
ctx.moveTo(fieldX_start, fieldY1);
ctx.lineTo(fieldX_end, fieldY1);
ctx.moveTo(fieldX_end, fieldY1);
ctx.lineTo(fieldX_end - arrowSize, fieldY1 - arrowSize / 2);
ctx.moveTo(fieldX_end, fieldY1);
ctx.lineTo(fieldX_end - arrowSize, fieldY1 + arrowSize / 2);
ctx.stroke();

// Arrow 2
ctx.beginPath();
ctx.moveTo(fieldX_start, fieldY2);
ctx.lineTo(fieldX_end, fieldY2);
ctx.moveTo(fieldX_end, fieldY2);
ctx.lineTo(fieldX_end - arrowSize, fieldY2 - arrowSize / 2);
ctx.moveTo(fieldX_end, fieldY2);
ctx.lineTo(fieldX_end - arrowSize, fieldY2 + arrowSize / 2);
ctx.stroke();

// Label for magnetic field
ctx.textAlign = 'center';
ctx.fillText('magnetic field', (fieldX_start + fieldX_end) / 2, fieldY2 + 35);

// --- Solenoid ---
const sol_x_start = 220;
const sol_y_top = 100;
const sol_diameter = 120;
const sol_y_bottom = sol_y_top + sol_diameter;
const num_loops_drawn = 10;
const loop_spacing = 22;
const sol_x_end = sol_x_start + num_loops_drawn * loop_spacing;

// Horizontal guide lines
ctx.beginPath();
ctx.moveTo(sol_x_start - loop_spacing, sol_y_top);
ctx.lineTo(sol_x_end + loop_spacing, sol_y_top);
ctx.moveTo(sol_x_start - loop_spacing, sol_y_bottom);
ctx.lineTo(sol_x_end + loop_spacing, sol_y_bottom);
ctx.stroke();

// Solenoid coils
ctx.beginPath();
// Draw all vertical lines
for (let i = 0; i <= num_loops_drawn; i++) {
    let x = sol_x_start + i * loop_spacing;
    ctx.moveTo(x, sol_y_top);
    ctx.lineTo(x, sol_y_bottom);
}
// Draw all top arcs
for (let i = 0; i < num_loops_drawn; i++) {
    let x_center = sol_x_start + i * loop_spacing + loop_spacing / 2;
    ctx.moveTo(sol_x_start + i * loop_spacing, sol_y_top);
    ctx.arc(x_center, sol_y_top, loop_spacing / 2, Math.PI, 0);
}
// Draw all bottom arcs
for (let i = 0; i < num_loops_drawn; i++) {
    let x_center = sol_x_start + i * loop_spacing + loop_spacing / 2;
    ctx.moveTo(sol_x_start + i * loop_spacing, sol_y_bottom);
    ctx.arc(x_center, sol_y_bottom, loop_spacing / 2, Math.PI, 0);
}
ctx.stroke();

// Lead wires
ctx.beginPath();
const lead_y_start = sol_y_bottom;
const lead_y_end = sol_y_bottom + 40;
ctx.moveTo(sol_x_start, lead_y_start);
ctx.lineTo(sol_x_start, lead_y_end);
ctx.moveTo(sol_x_end, lead_y_start);
ctx.lineTo(sol_x_end, lead_y_end);
ctx.stroke();

// --- Dimension for Diameter ---
const dim_line_x = sol_x_end + 70;

// Dashed lines from solenoid to dimension line
ctx.beginPath();
ctx.setLineDash([4, 4]);
ctx.moveTo(sol_x_end, sol_y_top);
ctx.lineTo(dim_line_x, sol_y_top);
ctx.moveTo(sol_x_end, sol_y_bottom);
ctx.lineTo(dim_line_x, sol_y_bottom);
ctx.stroke();
ctx.setLineDash([]); // Reset line dash

// Vertical dimension line with arrows
ctx.beginPath();
ctx.moveTo(dim_line_x, sol_y_top);
ctx.lineTo(dim_line_x, sol_y_bottom);
// Arrowheads
ctx.moveTo(dim_line_x, sol_y_top);
ctx.lineTo(dim_line_x - 4, sol_y_top + 6);
ctx.moveTo(dim_line_x, sol_y_top);
ctx.lineTo(dim_line_x + 4, sol_y_top + 6);
ctx.moveTo(dim_line_x, sol_y_bottom);
ctx.lineTo(dim_line_x - 4, sol_y_bottom - 6);
ctx.moveTo(dim_line_x, sol_y_bottom);
ctx.lineTo(dim_line_x + 4, sol_y_bottom - 6);
ctx.stroke();

// Dimension text
ctx.textAlign = 'left';
ctx.fillText('diameter', dim_line_x + 10, sol_y_top + sol_diameter / 2 - 10);
ctx.fillText('6.0 cm', dim_line_x + 10, sol_y_top + sol_diameter / 2 + 12);

// --- Label for Solenoid ---
const label_x_center = 510;
const label_y_top_line = 40;
const pointer_target_x = sol_x_start + (num_loops_drawn - 1.5) * loop_spacing;
const pointer_target_y = sol_y_top;
const pointer_start_x = label_x_center - 35;
const pointer_start_y = label_y_top_line + 10;

// Pointer line
ctx.beginPath();
ctx.moveTo(pointer_start_x, pointer_start_y);
ctx.lineTo(pointer_target_x, pointer_target_y);
ctx.stroke();

// Label text
ctx.textAlign = 'left';
ctx.fillText('solenoid', pointer_start_x, label_y_top_line - 5);
ctx.fillText('540 turns', pointer_start_x, label_y_top_line + 15);

// --- Figure Caption ---
ctx.textAlign = 'center';
ctx.fillText('Fig. 9.1', canvas.width / 2 - 20, 380);

</script>
</body>
</html>